#import "ImageTopButton.h"

@implementation ImageTopButton

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        [self commonInit];
    }
    return self;
}

- (instancetype)initWithCoder:(NSCoder *)coder {
    self = [super initWithCoder:coder];
    if (self) {
        [self commonInit];
    }
    return self;
}

- (void)commonInit {
    self.spacing = 5.0; // 默认间距为5点
    [self setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    self.titleLabel.textAlignment = NSTextAlignmentCenter;
    self.titleLabel.numberOfLines = 1; // 默认单行文本
}

- (CGRect)titleRectForContentRect:(CGRect)contentRect {
    NSString *title = [self titleForState:self.state];
    if (!title || title.length == 0) {
        return CGRectZero;
    }
    
    UIFont *font = self.titleLabel.font ?: [UIFont systemFontOfSize:14];
    CGSize titleSize = [title boundingRectWithSize:CGSizeMake(contentRect.size.width, CGFLOAT_MAX)
                                            options:NSStringDrawingUsesLineFragmentOrigin
                                         attributes:@{NSFontAttributeName: font}
                                            context:nil].size;
    
    titleSize.width = ceil(titleSize.width);
    titleSize.height = ceil(titleSize.height);
    
    // 计算文字区域Y坐标（图片下方 + 间距）
    UIImage *image = [self imageForState:self.state];
    CGFloat imageHeight = image ? image.size.height : 0;
    CGFloat titleY = imageHeight + self.spacing;
    
    // 水平居中
    CGFloat titleX = (contentRect.size.width - titleSize.width) / 2;
    
    return CGRectMake(titleX, titleY-10, titleSize.width, titleSize.height);
}

- (CGRect)imageRectForContentRect:(CGRect)contentRect {
    UIImage *image = [self imageForState:self.state];
    if (!image) {
        return CGRectZero;
    }
    
    CGSize imageSize = image.size;
    CGFloat imageX = (contentRect.size.width - imageSize.width) / 2;
    
    // 垂直方向：顶部对齐，或在有足够空间时居中
    CGFloat maxImageY = contentRect.size.height - imageSize.height - self.titleLabel.intrinsicContentSize.height;
    CGFloat imageY = 0;
    
    if (maxImageY > 0) {
        // 有足够空间，整体居中
        imageY = (maxImageY - self.spacing) / 2;
    }
    
    return CGRectMake(imageX, imageY-10, imageSize.width, imageSize.height);
}

- (CGSize)sizeThatFits:(CGSize)size {
    UIImage *image = [self imageForState:self.state];
    NSString *title = [self titleForState:self.state];
    
    CGSize imageSize = image ? image.size : CGSizeZero;
    CGSize titleSize = CGSizeZero;
    
    if (title && title.length > 0) {
        UIFont *font = self.titleLabel.font ?: [UIFont systemFontOfSize:14];
        titleSize = [title boundingRectWithSize:CGSizeMake(size.width, CGFLOAT_MAX)
                                        options:NSStringDrawingUsesLineFragmentOrigin
                                     attributes:@{NSFontAttributeName: font}
                                        context:nil].size;
        titleSize.width = ceil(titleSize.width);
        titleSize.height = ceil(titleSize.height);
    }
    
    // 计算总高度：图片高度 + 间距 + 文字高度
    CGFloat totalHeight = imageSize.height + self.spacing + titleSize.height;
    // 取图片和文字的最大宽度
    CGFloat totalWidth = MAX(imageSize.width, titleSize.width);
    
    // 添加一些额外的边距
    return CGSizeMake(totalWidth + 10, totalHeight + 10);
}

@end
